//  Copyright (c) 2010, Keith Nixon, Sales Engineering, Salesforce.com Inc.
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
//  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//  Neither the name of the salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
//  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//  *NB - This is NOT a bulkified trigger
//
//  History
//
//  Version  Date            Author          Comments
//  1.0      05-11-2010      Keith Nixon     Initial version
//

trigger SetOutletObjectivesAndProductsInsert on Account (after insert) {
    
    //1. We need to determine the record type of the account.
    Account contextAccount = Trigger.new.get(0);
    Id outletDefId = contextAccount.Outlet_definition__c;
    Id recordTypeId = contextAccount.recordTypeId;
    Boolean validRecordType = false;//A boolean that indicates if we are working with a record type that has an outlet definition lookup.
    String recordTypeName;
    
    if(recordTypeId != null)
    {
        recordTypeName = [
            Select Name From RecordType where Id =: recordTypeId LIMIT 1
        ].Name;
    }
    
    //match up the record type name
    if(recordTypeName != null)
    {   
        if(recordTypeName.equalsIgnoreCase('large outlet'))
        {
            validRecordType = true;
        }
        else if(recordTypeName.equalsIgnoreCase('medium outlet'))
        {
            validRecordType = true;
        }
        else if(recordTypeName.equalsIgnoreCase('outlet'))
        {
            validRecordType = true;
        }
        else if(recordTypeName.equalsIgnoreCase('small outlet'))
        {
            validRecordType = true;
        }
    }
    
    //2. If a matching relevant record type was found for this
    //   outlet (account) that allows us to link it to an outlet
    //   definition, then we query for all our objectives. The reason
    //   we query Objectives is that an outlet definition is related
    //   to an objective (via outlet definition objective object).
    
    if(validRecordType)
    {    
        System.assert(outletDefId != null, 'The Outlet Definition Id for this outlet is null');
        
        List<Outlet_Objectives__c> outletObjectives = new List<Outlet_Objectives__c>();
        List<Outlet_Stock__c> outletStockList = new List<Outlet_Stock__c>();
        
        //   We will query the Outlet Definition Objective table and pull 
        //   and Objective fields we need via a parent relationship. 
        //(Select From Outlet_Definition_Objectives__c Where Objective__c =: outletDefId)
        
        for(Outlet_Definition_Objective__c objective : [Select Id, Objective__c, Objective__r.Days_Left_Over__c 
                                                            From Outlet_Definition_Objective__c
                                                                Where Outlet_definition__c =: outletDefId])
        {
            //3. We will build up our list of outlet objectives from the above
            //   query, and insert into the database.
            
            Outlet_Objectives__c outletObj = new Outlet_Objectives__c();
            outletObj.Account__c = contextAccount.Id;
            outletObj.Objective__c = objective.Objective__c;
            
            outletObjectives.add(outletObj);
        }
        
        if(!outletObjectives.isEmpty())
        {
            insert outletObjectives;
        }
        
        // now loop through all the existing outlet definition products
        
        for(Outlet_Definition_Product_ADL__c outletDefProd : [Select Id, Product__c
                                                                  From Outlet_Definition_Product_ADL__c
                                                                      Where Outlet_definition__c =: outletDefId])
        {
            Outlet_Stock__c outletStock = new Outlet_Stock__c();
            outletStock.Account__c = contextAccount.Id;
            outletStock.Product__c = outletDefProd.Product__c;
            
            outletStockList.add(outletStock);
        } 
        
        if(!outletStockList.isEmpty())
        {
            insert outletStockList;
        }          
    }
}